<?php
/**
 *@fileoverview: [群博客] 
 *@author: 辛少普 <shaopu@staff.sina.com.cn>
 *@date: Thu Dec 02 12:36:48 GMT 2010
 *@copyright: sina
 */

class gb_biz_pageconfigformater
{
    private $_theme_id;                             //当前主题ID
    private $_theme_config = array();               //当前主题的配置数据
    private $_page_id;                              //当前页面
    
    private $_module_map = array();                 //页面模块顺序    array({容器ID}=>array(1,2,3))
    private $_module_option = array();              //单个模块的配置
    private $_result = array();
    
    private $_cid2type = array();
    
    private $_def_avil_module_map = array();             //配置中定义的可用模块列表
    
    /**
     * 构造函数
     *
     * @param int $theme_id     //
     * @param int $page_id
     * @param array $mod_map    //模块分布  array({容器ID} => array(1,2,3))
     * @return void
     */
    public function __construct($theme_id , $page_id , $mod_map)
    {
        $this->_theme_id = $theme_id;
        $this->_theme_config = ml_factory::load_theme_config($theme_id);        //当前主题的配置信息
        $this->_page_id = $page_id;
        $this->_module_map = $mod_map;
        
        
        //预计算本主题的所有模块信息
        foreach ($this->_theme_config['sc'] as $sc_id => $sc_conf)
        {
            $this->_def_avil_module_map[$sc_id] = $this->_parse_mod_list($sc_conf['module']);
        }
        return ;
    }
    /**
     * 检查sc容器数
     *
     * @return bool
     */
    public function check_container_num()
    {
        if(count($this->_theme_config['sc']) != count($this->_module_map))
            return false;
        else 
            return true;
    }
    
    public function set_cid2type($array)
    {
        $this->_cid2type = $array;
    }
    
    /**
     * 格式化用户配置
     *
     */
    public function format()
    {
        $aOkModuleMap = array();        //整理完的模块分布
        
        //所有SC容器
        foreach ($this->_module_map as $sc_id => $aModList)
        {
            $aModId = array();                //所有模块ID
            //系统配置 当前容器的配置
            $aDefScCfg = $this->_theme_config['sc'][$sc_id];
            $enable_custom_module = $aDefScCfg['enable_custom_module'];//是否允许使用自定义模块
            
            $aOkModuleMap[$sc_id] = array();
            //以下只处理可选模块
            foreach ($aModList as $mod_id)
            {
                if(!is_numeric($mod_id))
                    continue;
                    
                $is_custom_module = ml_tool_module::is_custom_module($mod_id);    //是否自定义模块
                $mod_avail = false;    //模块是否可用
                $aModId[] = $mod_id;
            
                //容器是否可以使用自定义模块
                if($is_custom_module && $enable_custom_module)
                {
                    $mod_avail = true;
                }
                //在可选模块范围
                else
                {
                    //自定义模块 检查模块类型是否相同
                    if( $is_custom_module && in_array($this->_cid2type[$mod_id] , $this->_def_avil_module_map[$sc_id][GB_MOD_POWER_OPTIONAL]))
                        $mod_avail = true;
                    //系统模块
                    else if(in_array($mod_id , $this->_def_avil_module_map[$sc_id][GB_MOD_POWER_OPTIONAL]) || in_array($mod_id , $this->_def_avil_module_map[$sc_id][GB_MOD_POWER_REQUIRE]))
                        $mod_avail = true;
                }
                
                 

                if($mod_avail)
                {
                    //记录各容器中都有哪些模块
                    $mod = array(
                        'id' => $mod_id,
                    );
                    if(isset($this->_module_option[$mod_id]))
                        $mod['option'] = $this->_module_option[$mod_id];
                    
                    $aOkModuleMap[$sc_id][] = $mod;
                }
                
            }//foreach mod

            //以下加入必选模块
            if(count($this->_def_avil_module_map[$sc_id][GB_MOD_POWER_REQUIRE]))
            {
                foreach ($this->_def_avil_module_map[$sc_id][GB_MOD_POWER_REQUIRE] as $mod_id)
                {
                    if(!in_array($mod_id , $aModId))
                    {
                        $mod = array(
                            'id' => $mod_id,
                        );
                        if(isset($this->_module_option[$mod_id]))
                            $mod['option'] = $this->_module_option[$mod_id];
                        
                        $aOkModuleMap[$sc_id][] = $mod;
                    }
                }
            }
            
            
        }//foreach sc
        
        $this->_result = $aOkModuleMap;
        return true;
    }
    /**
     * 取所有自定义模块ID
     *
     * @return bool
     */
    public function get_custom_mod_ids()
    {
        $aRs = array();
        foreach ($this->_module_map as $aModId)
        {
            foreach ($aModId as $mod_id)
            {
                if(gb_tool_module::is_custom_module($mod_id))
                    $aRs[] = $mod_id;
            }
        }
        return $aRs;
    }
    /**
     * 取所有系统模块ID
     *
     * @return array
     */
    public function get_sys_mod_ids()
    {
        $aRs = array();
        foreach ($this->_module_map as $aModId)
        {
            foreach ($aModId as $mod_id)
            {
                if(gb_tool_module::is_sys_module($mod_id))
                    $aRs[] = $mod_id;
            }
        }
        return $aRs;
    }
    /**
     * 为系统组件增加配置数据
     *
     * @param int $mod_id
     * @param array $option
     */
    public function add_module_option($mod_id , $option)
    {
        $this->_module_option[$mod_id] = $option;
    }
    /**
     * 生成用户配置数据
     *
     */
    public function get_result()
    {
        $aUserConfig = array(
            'theme_id' => $this->_theme_id,
            'module' => $this->_result,
        );
        return $aUserConfig;
    }
    
    /**
     * 分析配置文件中的模块情况
     * 哪些可选？哪些必选
     *
     * @param array $aDefineModule
     * @return array
     * 
     */
    private function _parse_mod_list($aDefineModule)
    {
        $aRs = array(
            GB_MOD_POWER_REQUIRE => array(),
            GB_MOD_POWER_OPTIONAL => array(),
        );
        

        foreach ($aDefineModule as $mod)
        {
            if($mod['power'] == GB_MOD_POWER_REQUIRE)
                $aRs[GB_MOD_POWER_REQUIRE][] = $mod['id'];
            else if($mod['power'] == GB_MOD_POWER_OPTIONAL)
            {
                if(!isset($mod['page_id']) || $mod['page_id'] == $this->_page_id)
                    $aRs[GB_MOD_POWER_OPTIONAL][] = $mod['id'];
            }
        }

        return $aRs;
    }
}
?>